package com.IntelligenceInsight.Myth.controller;

import com.IntelligenceInsight.Myth.model.MLJob;
import com.IntelligenceInsight.Myth.model.Algorithm;
import com.IntelligenceInsight.Myth.service.MLJobService;
import com.IntelligenceInsight.Myth.service.AlgorithmService;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
public class AlgorithmController {
    @Autowired
    private AlgorithmService algorithmService;
    @Autowired
    private MLJobService mlJobService;

    @RequestMapping(value = {"/algorithmAdd.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmAdd(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        String uuid = UUID.randomUUID().toString();
        String name = request.getParameter("name");
        String owner = request.getSession().getAttribute("username").toString();
        String description = request.getParameter("description");
        String jarPath = request.getParameter("jarPath");
        System.out.print(uuid + " " + name + " " + owner + " " + description);

        res = algorithmService.AlgorithmAdd(uuid, name, owner, description, jarPath);
        return res;
    }

    @RequestMapping(value = {"/algorithmUpdate.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmUpdate(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        NoteRequest res = algorithmService.AlgorithmByUuid(uuid);
        if(res.getStatus() != 0) {
            return res;
        }
        Algorithm algorithm = (Algorithm) res.getData();
        int lastVersion = algorithm.getVersion();
        int version = lastVersion + 1;
        String jarPath = request.getParameter("jarPath");

        res = algorithmService.AlgorithmUpdate(uuid, lastVersion, version, jarPath);

        return res;
    }


    @RequestMapping(value = {"/getAlgorithmListAll.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmListAll(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int count = algorithmService.AlgorithmListAllCount();
        int page_temp = page;
        int limit_temp = limit;
        if (count < page * limit) {
            limit = count - (page - 1) * limit;
        }
        page = (page_temp - 1) * limit_temp;
        NoteRequest res = algorithmService.AlgorithmListAll(page,limit);
        res.setCount(count);
        return res;
    }


    @RequestMapping(value = {"/AlgorithmListAllforName.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmListAllforName(HttpServletRequest request) {
        NoteRequest res = algorithmService.AlgorithmListAllforName();
        return res;
    }

    @RequestMapping(value = {"/getAlgorithmListByOwner.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmListByOwner(HttpServletRequest request) {
        String owner = request.getParameter("owner");
        NoteRequest res = algorithmService.AlgorithmListByOwner(owner);
        return res;
    }


    @RequestMapping(value = {"/getAlgorithmListMine.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmListMine(HttpServletRequest request) {
        System.out.println(request.getSession().getAttribute("username"));
        String owner = request.getSession().getAttribute("username").toString();
        NoteRequest res = algorithmService.AlgorithmListByOwner(owner);
        return res;
    }

    @RequestMapping(value = {"/getAlgorithmByUuid.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmByUuid(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        NoteRequest res = algorithmService.AlgorithmByUuid(uuid);
        return res;
    }

    @RequestMapping(value = {"/getAlgorithmByName.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmByName(HttpServletRequest request) {
        String name = request.getParameter("name");
        NoteRequest res = algorithmService.AlgorithmByName(name);
        return res;
    }


    /*@RequestMapping(value = {"/modelRemoveByName.do"}, method = RequestMethod.POST)
    public NoteRequest AlgorithmRemoveByName(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        String name = request.getParameter("name");
        res = algorithmService.AlgorithmByName(name);
        if(res.getStatus() != 0) {
            return res;
        }
        Algorithm model = (Algorithm) res.getData();
        String modelUuid = model.getUuid();
        res = mlJobService.MLJobByAlgorithm(modelUuid);
        if(res.getStatus() != 0) {
            return res;
        }
        List<MLJob> mljobs = (List<MLJob>) res.getData();
        for (MLJob mljob: mljobs) {
            if(!mljob.getState().equals("FINISH") && !mljob.getState().equals("FAIL")) {
                res.setStatus(1);
                res.setMsg("Job use this job not finished");
                res.setData(null);
                return res;
            }
        }

        res = algorithmService.AlgorithmRemoveByName(name);
        return res;
    }*/

}
